package com.whut.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.whut.mobilesafe.R;
import com.whut.mobilesafe.service.AutoKillService;
import com.whut.mobilesafe.utils.PrefUtils;
import com.whut.mobilesafe.utils.ServiceUtils;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/30 16:36
 * 版    本：1.0
 * 描    述：
 */
public class ProcessSettingActivity extends Activity {

    private CheckBox cbShowSystem;
    private CheckBox cbLockClean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_setting);

        cbShowSystem = (CheckBox) findViewById(R.id.cb_show_system);
        cbLockClean = (CheckBox) findViewById(R.id.cb_lock_clean);

        boolean showSystemProcess = PrefUtils.getBoolean("show_system_process", true, this);
        if(showSystemProcess){
            cbShowSystem.setChecked(showSystemProcess);
            cbShowSystem.setText("显示系统进程");
        }else{
            cbShowSystem.setChecked(showSystemProcess);
            cbShowSystem.setText("不显示系统进程");
        }

        cbShowSystem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cbShowSystem.setText("显示系统进程");
                    PrefUtils.putBoolean("show_system_process", true, getApplicationContext());
                } else {
                    cbShowSystem.setText("不显示系统进程");
                    PrefUtils.putBoolean("show_system_process", false, getApplicationContext());
                }
            }
        });

        boolean serviceRunning = ServiceUtils.isServiceRunning(this, "com.whut.mobilesafe.service.AutoKillService");
        if(serviceRunning){
            cbLockClean.setChecked(true);
            cbLockClean.setText("锁屏清理已开启");
        }else{
            cbLockClean.setChecked(false);
            cbLockClean.setText("锁屏清理已关闭");
        }
        cbLockClean.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent service = new Intent(getApplicationContext(), AutoKillService.class);
                if(!isChecked){
                    cbLockClean.setText("锁屏清理已关闭");
                    stopService(service);
                    boolean serviceRunning = ServiceUtils.isServiceRunning(getApplicationContext(), "com.whut.mobilesafe.service.AutoKillService");
                    Log.d("ProcessSettingActivity", "serviceRunning:----------" + serviceRunning);
                }else{
                    cbLockClean.setText("锁屏清理已开启");
                    startService(service);
                    boolean serviceRunning = ServiceUtils.isServiceRunning(getApplicationContext(), "com.whut.mobilesafe.service.AutoKillService");
                    Log.d("ProcessSettingActivity", "serviceRunning:----------" + serviceRunning);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
